package com.quanquan.store.repository.marketActivity.scratchCard;

import com.quanquan.store.entity.marketActivity.scratchCard.ScratchCardGift;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName ScratchCardGiftRepository
 * @Description
 * @Author Owen
 * @Date 2019/8/28 0028 上午 9:58
 * @Version V1.0
 **/
public interface ScratchCardGiftRepository extends JpaRepository<ScratchCardGift,Long>, JpaSpecificationExecutor<ScratchCardGift> {

    @Query(value = "select * from oo_scratch_card_gift where scratch_card_id=?1 and status=1 and remain_total > 0 and start_time <= now() and end_time > now()",nativeQuery = true)
    List<ScratchCardGift> findScratchCardGift(Long scratchCardId);

    @Query(value = "select sum(odds) from oo_scratch_card_gift where scratch_card_id=?1 and status=1 and remain_total > 0 and start_time <= now() and end_time > now()",nativeQuery = true)
    Integer sumGiftOdds(Long scratchCardId);

    @Transactional
    @Modifying
    @Query(value = "update oo_scratch_card_gift set remain_total = remain_total - 1 where id = ?1 and remain_total > 0",nativeQuery = true)
    Integer substractStock(Long giftId);

}
